package com.lck.blogmanager.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lck.blogmanager.entity.BlogCommentEntity;
import com.lck.blogmanager.entity.QuestionCommentEntity;
import com.lck.blogmanager.mapper.QuestionCommentMapper;
import com.lck.blogmanager.req.*;
import com.lck.blogmanager.res.UserBlogCommentPageRes;
import com.lck.blogmanager.res.UserQuestionCommentPageRes;
import com.lck.blogmanager.service.QuestionCommentService;
import com.lck.blogmanager.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author wyq
 * @version v1.0.0
 * @explain
 * @date 2024/4/7 17:18
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class QuestionCommentServiceImpl extends ServiceImpl<QuestionCommentMapper, QuestionCommentEntity> implements QuestionCommentService {

    @Override
    public Integer addQuestionComment(AddQuestionCommentReq req) {
        QuestionCommentEntity comment = new QuestionCommentEntity();
        if(req.getTargetId()!=null){
            QuestionCommentEntity questionComment = this.getById(req.getTargetId());
            comment.setTargetId(questionComment.getUserId());
        }
        comment.setUserId(SecurityUtils.getUserId());
        comment.setTargetId(req.getTargetId());
        comment.setBody(req.getBody());
        comment.setCreateTime(LocalDateTime.now());
        comment.setQuestionId(req.getQuestionId());
        this.getBaseMapper().insert(comment);
        return comment.getId();
    }

    @Override
    public Page<UserQuestionCommentPageRes> pageQuestionComment(PageQuestionCommentReq req) {
        Page page = new Page(req.getPageNum(), req.getPageSize());
        Page<UserQuestionCommentPageRes> info = this.getBaseMapper().getComments(req,page);
        return info;
    }

    @Override
    public void deleteComment(UserDeleteQuestionCommentReq req) {
        this.getBaseMapper().deleteById(req.getId());
    }

}
